﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Runtime.InteropServices;

namespace TfsBacklogMgr.Main
{
    public enum WITS
    {

    }
    public interface IFactory
    {
        Context LoadSettings(string filepath);
        void SaveSettings(Context ctx);
        void ConnectToTfs();
        void LoadWorkItems();
        List<StateAndReason> GetNextStatesFor(string WIT, string curState);
        
        string WITStory { get; }
        string WITTask { get; }
        string WITBug { get; }

        string WITFPoints { get; }
        string WITFHours { get; }
        string WITFRemain { get; }

        int PointsPrecision { get; }
        int HoursPrecision { get; }
        int RemainPrecision { get; }

    }

    public static class Factory
    {
        private static IFactory _factory = null;
        public static bool IsTesting { get; set; }

        public static IFactory GetFactory()
        {
            if (_factory == null)
            {
                if (IsTesting)
                {
                    _factory = new TestFactory();
                }
                else
                {
                    RegisterFileType();
                    _factory = new TfsFactory();
                }
            }
            return _factory;
        }

        #region File type integration

        [DllImport("shell32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);

        public static void RegisterFileType()
        {
            string appPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            string regName = Registry.CurrentUser.GetValue("Software\\Classes\\.backlog") as string;
            if( regName == null )
            {
                RegistryKey extKey = Registry.CurrentUser.CreateSubKey("Software\\Classes\\.backlog");
                extKey.SetValue("", "TFS Backlog Mgr file");

                RegistryKey iconKey = extKey.CreateSubKey("DefaultIcon");
                iconKey.SetValue("", appPath);
                iconKey.Close();

                RegistryKey shellKey = extKey.CreateSubKey("Shell");
                shellKey.CreateSubKey("open").CreateSubKey("command").SetValue("", string.Format("\"{0}\" \"%1\"", appPath) );
                shellKey.Close();
                extKey.Close();


                RegistryKey CurrentUser = Registry.CurrentUser.CreateSubKey(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.backlog");
                CurrentUser.SetValue("Progid", ".backlog", RegistryValueKind.String);
                CurrentUser.Close();

                // Tell explorer the file association has been changed
                SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero);

            }
        }

        #endregion

    }
}
